package com.xiebishe.mylibrary.net.retrofit;


import android.util.Log;

import com.xiebishe.mylibrary.common.LJToastUtils;

import org.jetbrains.annotations.NotNull;


public abstract class ExceptionHandler {
    public final void handleException(@NotNull Throwable e) {
        if (e instanceof ExceptionHandle.ResponeThrowable) {
            ExceptionHandle.ResponeThrowable responeThrowable = (ExceptionHandle.ResponeThrowable) e;
            if (responeThrowable.errorCode == RxERROR.NETWORK_ERROR) {
                if ("can not connect".equals(responeThrowable.message)) {
                    if (onNetDisconnectError(responeThrowable)) {
                        return;
                    }
                } else if (onNetWorkError(responeThrowable)) {
                    return;
                }
            } else if (responeThrowable.errorCode == RxERROR.UNAUTHORIZED_ERROR) {
                ////未登录错误
                if (onNotLoginError()) {
                    return;
                }

            } else if (responeThrowable.errorCode == RxERROR.HTTP_ERROR
                    || responeThrowable.errorCode == RxERROR.PARSE_ERROR
                    || responeThrowable.errorCode == RxERROR.SSL_ERROR) {

                //其他错误
                if (onOtherError(responeThrowable)) {
                    return;
                }

            } else if (responeThrowable.errorCode == RxERROR.RES_NOT_FOUND || responeThrowable.errorCode == RxERROR.RES_NOT_FOUND_2 || responeThrowable.errorCode == RxERROR.RES_IS_BLOCKED) {
                if (onResNotFoundError(responeThrowable)) {
                    return;
                }
            } else if (responeThrowable.errorCode == RxERROR.SERVER_MAINTENANCE) {

            } else {
                ////协议内错误
                if (onCustomError(responeThrowable)) {
                    return;
                }
            }
        } else {
            ExceptionHandle.ResponeThrowable responeThrowable = new ExceptionHandle.ResponeThrowable(e, RxERROR.UNKNOWN);
            if (onUnknowError(responeThrowable)) {
                return;
            }

        }
        onFinalError(e);

    }

    public boolean onResNotFoundError(@NotNull ExceptionHandle.ResponeThrowable responeThrowable) {
        return false;
    }

    public boolean onNotLoginError() {
        return false;
    }

    public boolean onCustomError(@NotNull ExceptionHandle.ResponeThrowable error) {
        return false;
    }

    public boolean onOtherError(@NotNull ExceptionHandle.ResponeThrowable error) {
        return false;
    }

    public boolean onUnknowError(@NotNull ExceptionHandle.ResponeThrowable error) {
        Log.w("TT", "onUnknowError: ", error);
        return false;
    }

    public boolean onNetWorkError(@NotNull ExceptionHandle.ResponeThrowable e) {
        return false;
    }

    public boolean onNetDisconnectError(@NotNull ExceptionHandle.ResponeThrowable e) {
        LJToastUtils.showToast("当前网络不可用，请检查网络情况");
        return false;
    }


    public void onFinalError(@NotNull Throwable e) {

    }

}
